Imports Clo = Enhancements.Core.Logging
Imports Ere = Enhancements.Basic.RegularExpressions
Imports Exe = Enhancements.Basic.Expressions
Imports Gen = System.Collections.Generic
Imports Glo = System.Globalization
Imports Iou = System.IO
Imports Ope = Enhancements.Basic.Expressions.Operators
Imports Pri = Enhancements.Basic.Primitives
Imports Ref = System.Reflection
Imports Reg = System.Text.RegularExpressions
Imports Sys = System
Imports Tex = System.Text
Imports Val = Enhancements.Basic.Expressions.Values
Imports Xsd = System.Xml.Schema
Imports W3c = System.Xml

Namespace Enhancements.Basic.Expressions.Functions.DateTimes
    ''' <summary>Adds a time interval to a date time value. The following interval types are supported:<list type="bullet"><item>Years: y</item><item>Months: M</item><item>Days: d</item><item>Hours: h</item><item>Minutes: m</item><item>Seconds: s</item><item>Milliseconds: l</item></list></summary>
    ''' <remarks>Supports the following signatures:<list type="bullet"><item>DateTimeAdd(Value As DateTime, ValueToAdd As Integer, ValueToAddType As String) As DateTime</item></list></remarks>
    <Serializable(), DebuggerStepThrough()> _
    Public NotInheritable Class DateTimeAdd
        Inherits [Function]

        Private Shared __Signatures() As FunctionSignature = Nothing
        Private Shared __SignatureDateTimeIntegerString As FunctionSignature = Nothing

        Shared Sub New()
            Dim _signaturesList As Gen.List(Of FunctionSignature) = New Gen.List(Of FunctionSignature)()
            __SignatureDateTimeIntegerString = New FunctionSignature(New FunctionSignatureParameter() {New FunctionSignatureParameter("Value", GetType(Val.DateTime)), New FunctionSignatureParameter("ValueToAdd", GetType(Val.Integer)), New FunctionSignatureParameter("ValueToAddType", GetType(Val.String))}, New FunctionSignatureResult(GetType(Val.DateTime)))
            _signaturesList.Add(__SignatureDateTimeIntegerString)
            __Signatures = _signaturesList.ToArray()
        End Sub

        ''' <summary>Initializes a DateTimeAdd instance.</summary>
        Public Sub New()
            MyBase.New("DateTimeAdd", __Signatures, Nothing)
        End Sub

        ''' <summary>Solves a function call by using the supplied signature and parameters values.</summary>
        ''' <param name="_signature">Signature specified.</param>
        ''' <param name="_parametersValues">Parameters values specified. If none, an empty array is passed.</param>
        ''' <remarks>The execution of this function implementations are considered to deliver correct results only when called through the expressions subsystem. If called directly by user code, the returned results may be incorrect.</remarks>
        ''' <exception cref="Sys.Exception">Any exception can be throwed by an implementation of this function as a result of the impossibility of generating a correct end result.</exception>
        ''' <returns> </returns>
        Public Overrides Function Solve(ByVal _signature As FunctionSignature, ByVal _parametersValues() As Sys.Object) As Sys.Object
            Dim _returnValue As Sys.Object = Nothing
            If _signature.Equals(__SignatureDateTimeIntegerString) Then
                _returnValue = __SolveSignatureDateTimeIntegerString(_parametersValues(0), _parametersValues(1), _parametersValues(2))
            End If

            Return _returnValue
        End Function

        Private Function __SolveSignatureDateTimeIntegerString(ByVal _value As Sys.Object, ByVal _valuetoadd As Sys.Object, ByVal _valuetype As Sys.Object) As Sys.DateTime
            Dim _valueCasted As Sys.DateTime = CType(_value, Sys.DateTime)
            Dim _valuetoaddCasted As Sys.Int64 = CType(_valuetoadd, Sys.Int64)
            Dim _valuetoaddtypeCasted As System.String = CType(_valuetype, System.String)
            Dim _valuetoaddtypesAllowed() As System.String = {"y", "M", "d", "h", "m", "s", "l"}
            _valuetoaddtypeCasted.IsIn("_valuetoaddtypeCasted", _valuetoaddtypesAllowed, True)
            Dim _returnValue As Sys.DateTime = Nothing
            If _valuetoaddtypeCasted.Equals("y", StringComparison.Ordinal) Then
                _returnValue = _valueCasted.AddYears(CType(_valuetoaddCasted, Sys.Int32))
            Else
                If _valuetoaddtypeCasted.Equals("M", StringComparison.Ordinal) Then
                    _returnValue = _valueCasted.AddMonths(CType(_valuetoaddCasted, Sys.Int32))
                Else
                    If _valuetoaddtypeCasted.Equals("d", StringComparison.Ordinal) Then
                        _returnValue = _valueCasted.AddDays(CType(_valuetoaddCasted, Sys.Int32))
                    Else
                        If _valuetoaddtypeCasted.Equals("h", StringComparison.Ordinal) Then
                            _returnValue = _valueCasted.AddHours(CType(_valuetoaddCasted, Sys.Int32))
                        Else
                            If _valuetoaddtypeCasted.Equals("m", StringComparison.Ordinal) Then
                                _returnValue = _valueCasted.AddMinutes(CType(_valuetoaddCasted, Sys.Int32))
                            Else
                                If _valuetoaddtypeCasted.Equals("s", StringComparison.Ordinal) Then
                                    _returnValue = _valueCasted.AddSeconds(CType(_valuetoaddCasted, Sys.Int32))
                                Else
                                    If _valuetoaddtypeCasted.Equals("l", StringComparison.Ordinal) Then
                                        _returnValue = _valueCasted.AddMilliseconds(CType(_valuetoaddCasted, Sys.Int32))
                                    End If
                                End If
                            End If
                        End If
                    End If
                End If
            End If

            Return _returnValue
        End Function

    End Class
End Namespace
